@(account: Option[gitbucket.core.model.Account], members: List[gitbucket.core.model.GroupMember])(implicit context: gitbucket.core.controller.Context)
@import context._
@import gitbucket.core.view.helpers._
@html.main(if(account.isEmpty) "Create group" else "Edit group"){
<div class="container">
  <form id="form" method="post" action="@if(account.isEmpty){@path/groups/new} else {@path/@account.get.userName/_editgroup}" validate="true">
      <div class="row-fluid">
        <div class="span5">
          <fieldset>
            <label for="groupName" class="strong">Group name</label>
            <div>
              <span id="error-groupName" class="error"></span>
            </div>
            <input type="text" name="groupName" id="groupName" value="@account.map(_.userName)"@if(account.isDefined){ readonly}/>
          </fieldset>
          <fieldset>
            <label class="strong">URL (Optional)</label>
            <div>
              <span id="error-url" class="error"></span>
            </div>
            <input type="text" name="url" id="url" style="width: 300px;" value="@account.map(_.url)"/>
          </fieldset>
          <fieldset>
            <label for="avatar" class="strong">Image (Optional)</label>
            @helper.html.uploadavatar(account)
          </fieldset>
        </div>
        <div class="span7">
          <fieldset>
            <label class="strong">Members</label>
            <ul id="member-list" class="collaborator">
            </ul>
            @helper.html.account("memberName", 200)
            <input type="button" class="btn" value="Add" id="addMember"/>
            <input type="hidden" id="members" name="members" value="@members.map(member => member.userName + ":" + member.isManager).mkString(",")"/>
            <div>
              <span class="error" id="error-members"></span>
            </div>
          </fieldset>
        </div>
      </div>
      <fieldset class="margin">
        @if(account.isDefined){
          <div class="pull-right">
            <a href="@url(account.get.userName)/_deletegroup" id="delete" class="btn btn-danger">Delete Group</a>
          </div>
        }
        <input type="submit" class="btn btn-success" value="@if(account.isEmpty){Create Group} else {Update Group}"/>
        @if(account.isDefined){
          <a href="@url(account.get.userName)" class="btn">Cancel</a>
        }
      </fieldset>
  </form>
</div>
}
<script>
$(function(){
  $('input[type=submit]').click(function(){
    updateMembers();
  });

  $('#addMember').click(function(){
    $('#error-members').text('');
    var userName = $('#memberName').val();

    // check empty
    if($.trim(userName) == ''){
      return false;
    }

    // check duplication
    var exists = $('#member-list li').filter(function(){
      return $(this).data('name') == userName;
    }).length > 0;
    if(exists){
      $('#error-members').text('User has been already added.');
      return false;
    }

    // check existence
    $.post('@path/_user/existence', {
      'userName': userName
    }, function(data, status){
      if(data == 'true'){
        addMemberHTML(userName, false);
      } else {
        $('#error-members').text('User does not exist.');
      }
    });
  });

  $(document).on('click', '.remove', function(){
    $(this).parent().remove();
  });

  // Don't submit form by ENTER key
  $('#memberName').keypress(function(e){
    return !(e.keyCode == 13);
  });

  $('#delete').click(function(){
    return confirm('Once you delete this group, there is no going back.\nAre you sure?');
  });

  @members.map { member =>
    addMemberHTML('@member.userName', @member.isManager);
  }

  function addMemberHTML(userName, isManager){
    var memberButton = $('<button type="button" class="btn btn-default btn-mini" value="false">Member</button>').data('name', userName);
    if(!isManager){
      memberButton.addClass('active');
    }
    var managerButton = $('<button type="button" class="btn btn-default btn-mini" value="true">Manager</button>').data('name', userName);
    if(isManager){
      managerButton.addClass('active');
    }

    $('#member-list').append($('<li>')
      .data('name', userName)
      .append($('<div class="btn-group is_manager" data-toggle="buttons-radio">')
        .append(memberButton)
        .append(managerButton))
      .append(' ')
      .append($('<a>').attr('href', '@path/' + userName).text(userName))
      .append(' ')
      .append($('<a href="#" class="remove pull-right">(remove)</a>')));
  }

  function updateMembers(){
    var members = $('#member-list li').map(function(i, e){
      var userName = $(e).data('name');
      return userName + ':' + $('button.active').filter(function(i, e){
        return $(e).data('name') == userName;
      }).attr('value');
    }).get().join(',');
    $('#members').val(members);
  }
});
</script>